// pages/user/shopDes/index.js
var app = getApp()
var that
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgUrl:'https://img1.baidu.com/it/u=3647522616,1229934719&fm=26&fmt=auto&gp=0.jpg',//商铺图片
    statusBarHeight: app.globalData.height + 'px',
    navigationBarHeight: (app.globalData.height + 44 + 50) + 'px',
    active:1,//1点餐2评价3商家
    dcTypeList:['热销','套餐','拌饭','小菜【蔬菜】1111','烤肉','面食','必选'],
    dcTypeIndex:0,
    isShow:false,//弹框-规格
    kouwei:0,
    tjList:[
      {id:5,name:'拌饭套餐132323233232',money:10,num:0},
      {id:6,name:'韩国拌饭2',money:13,num:0},
      {id:7,name:'韩国拌饭3',money:20,num:0}
    ],
    list:[
      {id:1,name:'拌饭套餐111111111',money:20,num:0,guige:false},
      {id:2,name:'拌饭套餐2',money:30,num:0,guige:false},
      {id:3,name:'拌饭套餐3',money:40,num:0,guige:false},
      {id:4,name:'拌饭套餐4',money:10,num:0,guige:true,text:null,
        children:[
          {name:'规格',val:0,list:['15只/份']},
          {name:'口味',val:0,list:['广式香肠1','广式香肠2','广式香肠3','广式香肠4']}
        ]
      }
    ],
    more:false,
    totalFd:20,
    changeList:[],
    formGg:{},
    indexGg:-1,
    textGg:null,
    hide_good_box:true,
    isCon:true,
    pjTabList: ['全部 146','最新','好评 142','差评 1','有图 50','味道好 55'],
    pjTab:0,
    pjList:[//评价列表
      {
        name:'张三',
        image:[
          'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1029845672,1309686948&fm=11&gp=0.jpg',
          'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3062925006,36536704&fm=26&gp=0.jpg'
        ]
      },
      {
        name:'李四',
        image:[
          'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2182635746,1387809950&fm=26&gp=0.jpg',
          'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1533007948,1202546778&fm=15&gp=0.jpg',
          'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2821357017,1811748137&fm=26&gp=0.jpg'
        ]
      }
    ],
    total:10
  },
  getMore(){
    //加载更多
    if(this.data.totalFd>this.data.list.length&&!this.data.more){
      this.setData({
        more:true
      })
      setTimeout(()=>{
        let arr=[
          {id:1,name:'拌饭套餐111111111',money:20,num:0,guige:false},
          {id:2,name:'拌饭套餐2',money:30,num:0,guige:false},
          {id:3,name:'拌饭套餐3',money:40,num:0,guige:false},
          {id:4,name:'拌饭套餐4',money:10,num:0,guige:true,text:null,
            children:[
              {name:'规格',val:0,list:['15只/份']},
              {name:'口味',val:0,list:['广式香肠1','广式香肠2','广式香肠3','广式香肠4']}
            ]
          }]
        let data=this.data.list.concat(arr)
        this.setData({
          list:data,
          more:false
        })
      },1000)
    }
  },
  //点击--去结算
  goJiesuan(){
    this.setData({
      dcTypeIndex:6//必选下标
    })
    //必选选过后再跳转页面
    wx.navigateTo({
      url: '/pages/user/orderSure/index',
    })
  },
  yulanImg(event){
    //预览图片
    var dataset=event.currentTarget.dataset
    var pjList = this.data.pjList
    wx.previewImage({
      current: dataset.src, // 当前显示图片的http链接
      urls: pjList[dataset.index].image // 需要预览的图片http链接列表
    })
  },
  changePjTab(e){
    var index=e.currentTarget.dataset.index
    this.setData({
      pjTab:index
    })
  },
  // 2-24 radio点击事件
  checkedTap () {
    var checked = this.data.isCon
    this.setData({
      isCon: !checked
    })
  },
  changeGuigeVal(e){
    var index=e.currentTarget.dataset.index
    var idx=e.currentTarget.dataset.idx
    var list = this.data.list
    var formGg = this.data.formGg
    let textGg=[]
    formGg.children[index].val=idx
    list[this.data.indexGg].children[index].val=idx

    formGg.children.forEach((i)=>{
      textGg.push(i.list[i.val])
    })

    this.setData({
      list,
      formGg,
      textGg:textGg.join('/')
    })

  },
  //商品规格切换
  setGuige(event){
    var dataset=event.currentTarget.dataset
    this.setData({
      kouwei:dataset.index
    })
  },
  //tab切换
  changeTab:function(event){
    var dataset=event.currentTarget.dataset
    this.setData({
      active:dataset.type
    })
  },
  //热销切换
  changeType:function(event){
    var dataset=event.currentTarget.dataset
    this.setData({
      dcTypeIndex:dataset.index
    })
  },
  //关闭/打开 弹框
  changeShow(event){
    var dataset=event.currentTarget.dataset
    if(dataset.val){
      var textGg=[]
      dataset.item.children.forEach((i)=>{
        console.log(i.list[i.val])
        textGg.push(i.list[i.val])
      })
      
      this.setData({
        isShow:dataset.val,
        formGg:dataset.item,
        indexGg:dataset.index,
        textGg:textGg.join('/')
      })
    }else{
      this.setData({
        isShow:dataset.val,
        formGg:{},
        indexGg:-1,
        textGg:null
      })
    }
    
    

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.busPos = {};
    this.busPos['x'] = 0;//购物车的位置
    this.busPos['y'] = app.globalData.hh;

  },
  //规格选好--添加购物车
  guigeAddShopCar(e){
    this.touchOnGoods(e)
    this.setData({
      isShow:false,
      formGg:{},
      indexGg:-1
    })
  },
  //购物车--清空
  clearShopCar(){
    this.setData({
      changeList:[],
      tjList:[
        {id:5,name:'韩国拌饭1',money:10,num:0},
        {id:6,name:'韩国拌饭2',money:13,num:0},
        {id:7,name:'韩国拌饭3',money:20,num:0}
      ],
      list:[
        {id:1,name:'拌饭套餐1',money:20,num:0},
        {id:2,name:'拌饭套餐2',money:30,num:0},
        {id:3,name:'拌饭套餐3',money:40,num:0},
        {id:4,name:'拌饭套餐4',money:10,num:0,guige:true,text:null,
          children:[
            {name:'规格',val:0,list:['15只/份']},
            {name:'口味',val:0,list:['广式香肠1','广式香肠2','广式香肠3','广式香肠4']}
          ]
        }
      ]
    })
  },
  //购物车累加或移除-----子传父
  touchOnAddGoods(e){
    
    let data = JSON.parse(JSON.stringify(e.detail))
    this.data.list.forEach((i,index)=>{
      if(i.id==data.currentTarget.dataset.item.id){
        data.currentTarget.dataset.type='1'
        data.currentTarget.dataset.index=index
        
        console.log(index,i.num)
        if(data.currentTarget.dataset.fnc=='1'){
          //添加
          this.touchOnGoods(data)
        }else{
          //移除
          if(data.currentTarget.dataset.item.guige){
            //商品有规格的移除
            this.shopCarGgDataInit(data)
          }else{
            this.shopCarDataInit(data)
          }
        }
      }
    })
    this.data.tjList.forEach((i,index)=>{
      if(i.id==data.currentTarget.dataset.item.id){
        data.currentTarget.dataset.type='2'
        data.currentTarget.dataset.index=index
        data.currentTarget.dataset.item=i
        console.log(index,i.num)
        if(data.currentTarget.dataset.fnc=='1'){
          //添加
          this.touchOnGoods(data)
        }else{
          //移除
          this.shopCarDataInit(data)
        }
        
      }
    })
  },
  //点击-添加购物车
  touchOnGoods: function (e) {
    this.finger = {}; var topPoint = {};
    this.finger['x'] = e.touches["0"].clientX-20;//点击的位置
    this.finger['y'] = e.touches["0"].clientY-20;

    if (this.finger['y'] < this.busPos['y']) {
      topPoint['y'] = this.finger['y'] - 150;
    } else {
      topPoint['y'] = this.busPos['y'] - 150;
    }
    topPoint['x'] = Math.abs(this.finger['x'] - this.busPos['x']) / 2;

    if (this.finger['x'] > this.busPos['x']) {
      topPoint['x'] = (this.finger['x'] - this.busPos['x']) / 2 + this.busPos['x'];
    } else {//
      topPoint['x'] = (this.busPos['x'] - this.finger['x']) / 2 + this.finger['x'];
    }

    this.linePos = app.bezier([this.busPos, topPoint, this.finger], 30);
    this.startAnimation(e);
  },
  //开始动画
  startAnimation: function (e) {
    var index = 0, that = this,
      bezier_points = that.linePos['bezier_points'];

    this.setData({
      hide_good_box: false,
      bus_x: that.finger['x'],
      bus_y: that.finger['y']
    })
    var len = bezier_points.length;
    index = len
    this.timer = setInterval(function () {
      for(let i = index - 1; i > -1; i--) {
        that.setData({
          bus_x: bezier_points[i]['x'],
          bus_y: bezier_points[i]['y']
        })
        if (i < 1) {
          clearInterval(that.timer);
          let item = e.currentTarget.dataset.item

          if(item.guige){
            //商品有规格的添加
            that.shopCarGgDataInit(e)
          }else{
            that.shopCarDataInit(e)
          }
          

        }
      }
    }, 25);
  },
  shopCarGgDataInit(e){
    //处理特殊有规格的商品添加或移除
    let item = e.currentTarget.dataset.item
    let index = e.currentTarget.dataset.index
    let type = e.currentTarget.dataset.type
    let fnc = e.currentTarget.dataset.fnc
    let list = this.data.list
    let tjList = this.data.tjList
    let changeList = this.data.changeList

    fnc=='1'?item.num++:item.num--
    if(item.num<0) return false

    let text = item.children?this.data.textGg:item.text
    let a
    list.forEach((i)=>{
      if(i.id==item.id){
        fnc=='1'?i.num++:i.num--
      }
    })
    changeList.forEach((i,index)=>{
      if(i.id==item.id&&i.text==text){
        if(item.num==0){
          changeList.splice(index,1)
        }else{
          i.num=item.num
        }
        a=true
      }
    })
    if(!a){
      let obj = {
        id:item.id,
        name:item.name,
        text:text,
        num:1,
        money:item.money,
        guige:true
      }
      changeList.push(obj)
    }

    this.setData({
      hide_good_box: true,
      changeList,
      list,
      tjList
    })
  },
  shopCarDataInit(e){
    //商品的添加移除数据操作
    let item = e.currentTarget.dataset.item
    
    let index = e.currentTarget.dataset.index
    let type = e.currentTarget.dataset.type
    let fnc = e.currentTarget.dataset.fnc
    let list = this.data.list
    let tjList = this.data.tjList
    let changeList = this.data.changeList
    let flag = false
    
    fnc=='1'?item.num++:item.num--
   
    if(item.num<0) return false
    console.log(type)
    if(type=='1'){
      //具体订餐列表
      //替换
      list.splice(index, 1, item)
    }else{
      //商家推荐列表
      tjList.splice(index, 1, item)//替换
    }
    
    if(changeList.length>0){
      changeList.forEach((i,index)=>{
        if(i.id==item.id){
          //累加商品
          if(item.num==0){
            changeList.splice(index,1)
          }else{
            changeList[index]=item
          }
          
          flag = true
        }
      })
      if(!flag){changeList.push(item)}
    }else{
      changeList.push(item)
    }
    console.log(changeList)

    this.setData({
      hide_good_box: true,
      changeList,
      list,
      tjList
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(this.data.active==2){
      if(this.data.total>this.data.pjList.length){
        setTimeout(()=>{
          let arr=[//评价列表
            {
              name:'张三',
              image:[
                'https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1029845672,1309686948&fm=11&gp=0.jpg',
                'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3062925006,36536704&fm=26&gp=0.jpg'
              ]
            },
            {
              name:'李四',
              image:[
                'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2182635746,1387809950&fm=26&gp=0.jpg',
                'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1533007948,1202546778&fm=15&gp=0.jpg',
                'https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=2821357017,1811748137&fm=26&gp=0.jpg'
              ]
            }
          ]
          let data=this.data.pjList.concat(arr)
          this.setData({
            pjList:data
          })
        },1000)
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})